package com.iot.system.core.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iot.common.core.page.PageDomain;
import com.iot.common.core.page.TableSupport;
import com.iot.common.utils.bean.BeanUtils;
import com.iot.system.core.CoreDeviceYieldsService;
import com.iot.system.domain.DeviceDetails;
import com.iot.system.domain.DeviceState;
import com.iot.system.domain.DeviceYields;
import com.iot.system.domain.vo.DeviceYieldsVo;
import com.iot.system.service.IDeviceDetailsService;
import com.iot.system.service.IDeviceYieldsService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.iot.common.utils.PageUtils.startPage;


@Service
public class CoreDeviceYieldsServiceImpl implements CoreDeviceYieldsService {

    @Resource
    private IDeviceYieldsService iDeviceYieldsService;

    @Resource
    private IDeviceDetailsService iDeviceDetailsService;

    @Override
    public PageInfo<DeviceYieldsVo> selectDeviceYieldsList(DeviceYields deviceYields) {

        PageDomain pageDomain = TableSupport.getPageDomain();
        PageHelper.startPage(pageDomain.getPageNum(), pageDomain.getPageSize(), pageDomain.getOrderBy());
        List<DeviceYields> deviceYieldsList = iDeviceYieldsService.selectDeviceYieldsList(deviceYields);
        PageInfo<DeviceYields> pageInfo=new PageInfo<>(deviceYieldsList,5);
        if (CollectionUtils.isEmpty(deviceYieldsList)){
            return new PageInfo<>();
        }
        Map<Long, DeviceDetails> detailsMap = getLongDeviceDetailsMap(deviceYieldsList);
        List<DeviceYieldsVo> deviceYieldsVos = deviceYieldsList.stream().map(item -> {
            DeviceYieldsVo deviceYieldsVo = new DeviceYieldsVo();
            BeanUtils.copyProperties(item, deviceYieldsVo);
            if (detailsMap.containsKey(item.getDeviceId())) {
                deviceYieldsVo.setDeviceName(detailsMap.get(item.getDeviceId()).getDeviceName());
                deviceYieldsVo.setDeviceTemsCode(detailsMap.get(item.getDeviceId()).getDeviceTemsCode());
            }
            return deviceYieldsVo;
        }).collect(Collectors.toList());
        PageInfo<DeviceYieldsVo> pageYieldsInfo=new PageInfo<>(deviceYieldsVos,5);
        pageYieldsInfo.setPageNum(pageInfo.getPageNum());
        pageYieldsInfo.setPageSize(pageInfo.getPageSize());
        pageYieldsInfo.setTotal(pageInfo.getTotal());
        return pageYieldsInfo;
    }

    private Map<Long, DeviceDetails> getLongDeviceDetailsMap(List<DeviceYields> stateLogsList) {
        List<Long> deviceIds = stateLogsList.stream().map(DeviceYields::getDeviceId).collect(Collectors.toList());
        List<DeviceDetails> deviceDetails = iDeviceDetailsService.selectDeviceNameAndTemsCodeByIds(deviceIds);
        Map<Long, DeviceDetails> detailsMap = deviceDetails.stream().collect(Collectors.toMap(item -> item.getId(), item -> item));
        return detailsMap;
    }

    @Override
    public DeviceYields selectDeviceYieldsById(Long id) {
        return iDeviceYieldsService.selectDeviceYieldsById(id);
    }

    @Override
    public int insertDeviceYields(DeviceYields deviceYields) {
        return iDeviceYieldsService.insertDeviceYields(deviceYields);
    }

    @Override
    public int updateDeviceYields(DeviceYields deviceYields) {
        return iDeviceYieldsService.updateDeviceYields(deviceYields);
    }

    @Override
    public int deleteDeviceYieldsByIds(Long[] ids) {
        return iDeviceYieldsService.deleteDeviceYieldsByIds(ids);
    }
}
